﻿using AutoMapper.Internal.Mappers;
using CSRedis;
using Mes.Pendant.Common.DtoMapping;
using Mes.Pendant.MaterialTypes;
using Mes.Pendant.Workstation.Dto;
using Mes.Pendant.Workstation.IAppService;
using Mes.Pendant.WorkstationFiles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Mes.Pendant.Workstation
{
	public class WorkstationAppService : ApplicationService, IWorkstationAppService
	{
		private readonly IRepository<WorkstationFiles.Workstation, int> _repository;
		//	private readonly CSRedisClient csRedisClient = new CSRedisClient("localhost:6379");
		private static object obj = new object();

		public WorkstationAppService(IRepository<WorkstationFiles.Workstation, int> repository)
		{
			_repository = repository;
		}

		/// <summary>
		/// 工作站列表、分页、查询
		/// </summary>
		/// <param name="workstationName">工作站名称</param>
		/// <param name="page">当前页</param>
		/// <param name="limit">每页条数</param>
		/// <returns>工作站列表数据</returns>
		public async Task<PagedResult> GetWorkstationList(string? workstationName, int page = 1, int limit = 10)
		{
			var predicate = PredicateBuilder.New<WorkstationFiles.Workstation>(true);
			if (!string.IsNullOrEmpty(workstationName))
			{
				predicate = predicate.And(m => m.WorkstationName.Contains(workstationName));
			}
			var workstationList = _repository.GetListAsync(predicate).Result;

			PagedResult responseResult = new PagedResult();
			responseResult.CurrentPage = page;
			responseResult.PageSize = limit;
			responseResult.RowCount = workstationList.Count();
			responseResult.PageCount = (int)Math.Ceiling((responseResult.RowCount) / (double)limit);
			responseResult.Queryable = workstationList.OrderBy(p => p.Id).Skip((page - 1) * limit).Take(limit).AsQueryable();
			responseResult.Queryable = responseResult.Queryable.MapToList<WorkstationListDto>().AsQueryable();
			return responseResult;
		}

		/// <summary>
		/// 新增工作站
		/// </summary>
		/// <param name="addWorkstationDto">新增工作站实体</param>
		/// <returns>实体自增主键</returns>
		public async Task<int> AddWorkstation(AddWorkstationDto addWorkstationDto)
		{
			//判断
			var workstationCodeIsExist = _repository.AnyAsync(m => m.WorkstationCode == addWorkstationDto.WorkstationCode).Result;
			if (workstationCodeIsExist)
			{
				return -1;
			}

			var workstationNameIsExist = _repository.AnyAsync(m => m.WorkstationName == addWorkstationDto.WorkstationName).Result;
			if (workstationNameIsExist)
			{
				return -2;
			}

			var workstationModel = addWorkstationDto.MapTo<WorkstationFiles.Workstation>();
			await _repository.InsertAsync(workstationModel);
			return addWorkstationDto.Id;
		}

		/// <summary>
		/// 删除工作站
		/// </summary>
		/// <param name="id">实体主键</param>
		/// <returns>受影响的行数</returns>
		public async Task<int> RemoveWorkstationById(int id)
		{
			await _repository.DeleteAsync(id);
			return id;
		}

		/// <summary>
		/// 查看工作站详情
		/// </summary>
		/// <param name="id">工作站主键</param>
		/// <returns>工作站实体</returns>
		public async Task<EditWorkstationDto> FindWorkstationById(int id)
		{
			WorkstationFiles.Workstation workstationEntity = await _repository.GetAsync(id);
			//var model = WorkstationFiles.Workstation.MapTo<EditWorkstationDto>();
			EditWorkstationDto workstationModel = ObjectMapper.Map<WorkstationFiles.Workstation, EditWorkstationDto>(workstationEntity);
			return workstationModel;
		}

		/// <summary>
		/// 更新工作站
		/// </summary>
		/// <param name="editWorkstationDto">更新工作站实体</param>
		/// <returns>受影响的行数</returns>
		public async Task<int> UpdateWorkstation(EditWorkstationDto editWorkstationDto)
		{
			//var predicate = PredicateBuilder.New<WorkstationFiles.Workstation>(true);
			//predicate = predicate.And(m => m.WorkstationName == editWorkstationDto.WorkstationName ||
			//													m.WorkstationCode == editWorkstationDto.WorkstationCode &&
			//													m.Id != editWorkstationDto.Id);
			//var workstationList = _repository.GetListAsync(predicate).Result;

			var workstationNameIsExits = _repository.AnyAsync(m => m.WorkstationName == editWorkstationDto.WorkstationName ||
																m.WorkstationCode == editWorkstationDto.WorkstationCode &&
																m.Id != editWorkstationDto.Id).Result;
			if (workstationNameIsExits)
			{
				return -1;
			}
			WorkstationFiles.Workstation workstationModel = editWorkstationDto.MapTo<WorkstationFiles.Workstation>();
			await _repository.UpdateAsync(workstationModel);
			//var workstationModel = ObjectMapper.Map<EditWorkstationDto, WorkstationFiles.Workstation>(editWorkstationDto);
			//await _repository.UpdateAsync(workstationModel);
			return editWorkstationDto.Id;
		}

		public async Task<List<WorkstationListDto>> GetWorkstationListRedis()
		{

			var workstationList = Common.Redis.RedisHelper.Get<List<WorkstationFiles.Workstation>>("GetWorkstationListRedis");
			if (workstationList == null)
			{
				lock (obj)
				{
					if (workstationList == null)
					{
						workstationList = _repository.GetListAsync().Result;
						var seconds = 3000 + new Random().Next(1, 1000);
						Common.Redis.RedisHelper.Set("GetWorkstationListRedis", workstationList);
					}
				}
			}
			List<WorkstationListDto> workstationListDto = workstationList.MapToList<WorkstationListDto>();
			return workstationListDto;
		}





	}
}

